/*
 * This file Copyright (c) 2015. Walle.
 * (http://www.wallellen.com). All rights reserved.
 *
 *
 * This file is dual-licensed under both the
 * Walle Agreement (WA) and the GNU General Public License.
 * You may elect to use one or the other of these licenses.
 *
 * This file is distributed in the hope that it will be
 * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
 * implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
 * Redistribution, except as permitted by whichever of the GPL
 * or WA you select, is prohibited.
 *
 * 1. For the GPL license (GPL), you can redistribute and/or
 * modify this file under the terms of the GNU General
 * Public License, Version 3, as published by the Free Software
 * Foundation.  You should have received a copy of the GNU
 * General Public License, Version 3 along with this program;
 * if not, write to the Free Software Foundation, Inc., 51
 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * 2. For the Walle Agreement (WA), this file
 * and the accompanying materials are made available under the
 * terms of the WA which accompanies this distribution, and
 * is available at http://www.wallellen.com/agreement.html
 *
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
 */

package com.wallellen.web.easy;

import com.wallellen.core.sql.query.DBSort;
import com.wallellen.core.sql.query.DataQuery;
import com.wallellen.core.sql.result.DataResult;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Created by walle on 12/9/15.
 */
public class EasyUiUtils {
    private static String EDITOR_TYPE_TEXT = "text";

    private static String EDITOR_TYPE_SELECT = "numberbox";

    private static String EDITOR_TYPE_CHECKBOX = "{ \"type\":\"checkbox\", \"options\":{\"on\":true,\"off\":false} }";

    private static String EDITOR_TYPE_NUMBERBOX = "numberbox";

    public static Map<String, Object> formatGridData(DataResult result) {
        Map map = new HashMap();
        map.put("total", Integer.valueOf(result.getTotalSize()));
        map.put("rows", result.getResultList());
        return map;
    }

    public static List<Map<String, Object>> formatPropertygridData(List<Map<String, Object>> list, String nameIndex, String valueIndex, String GroupIndex, String editorType, String ruleIndex, String idIndex) {
        List returnList = new ArrayList();

        for (Iterator iterator = list.iterator(); iterator.hasNext(); ) {
            Map map = (Map) iterator.next();
            Map easyMap = new HashMap();
            easyMap.put("name", map.get(nameIndex).toString());
            easyMap.put("value", map.get(valueIndex).toString());
            easyMap.put("group", map.get(GroupIndex).toString());
            easyMap.put("id", map.get(idIndex).toString());

            Object editor = EDITOR_TYPE_TEXT;
            if ("2".equals(map.get(editorType).toString())) {
                editor = EDITOR_TYPE_SELECT;
            }
            if (("3".equals(map.get(editorType).toString())) &&
                    (map.get(ruleIndex) != null)) {
                editor = getComboboxOptions(map.get(ruleIndex).toString());
            }

            if ("4".equals(map.get(editorType).toString())) {
                Map type = new HashMap();
                type.put("type", "checkbox");
                Map type2 = new HashMap();
                type2.put("on", Boolean.valueOf(true));
                type2.put("off", Boolean.valueOf(false));
                type.put("options", type2);
                editor = type;
            }

            easyMap.put("editor", editor);
            returnList.add(easyMap);
        }

        return returnList;
    }

    private static Map<String, Object> getComboboxOptions(String Rules) {
        Map map1 = new HashMap();

        String[] ruleArray = Rules.split(",");
        List ruleList = new ArrayList();
        for (int i = 0; i < ruleArray.length; i++) {
            Map nodeMap = new HashMap();
            nodeMap.put("value", ruleArray[i]);
            nodeMap.put("text", ruleArray[i]);
            ruleList.add(nodeMap);
        }
        Map mapOptions = new HashMap();
        mapOptions.put("data", ruleList);
        mapOptions.put("panelHeight", "auto");
        map1.put("type", "combobox");
        map1.put("options", mapOptions);
        return map1;
    }

    public static List<EasyUiTreeNode> formatAjaxTree(List<Map<String, Object>> result1, String parentIdIndex, String idIndex, String titleIndex, String checkIndex) {
        return EasyUiTreeNode.formatAjaxTree(result1, parentIdIndex, idIndex, titleIndex, checkIndex, null);
    }

    public static List<EasyUiTreeNode> formatAjaxTree(List<Map<String, Object>> result1, String parentIdIndex, String idIndex, String titleIndex) {
        return EasyUiTreeNode.formatAjaxTree(result1, parentIdIndex, idIndex, titleIndex);
    }

    public static List<EasyUiTreeNode> formatAsyncAjaxTree(List<Map<String, Object>> result1, List<Map<String, Object>> result2, String parentIdIndex, String idIndex, String titleIndex, String imgIndex) {
        return EasyUiTreeNode.formatAsyncAjaxTree(result1, result2, parentIdIndex, idIndex, titleIndex, imgIndex);
    }

    public static DataQuery formatGridQuery(HttpServletRequest request, DataQuery query) {
        if (query == null) {
            query = new DataQuery();
        }
        if (request.getParameter("page") != null) {
            query.setCurrentPage(request.getParameter("page").toString());
        }
        if (request.getParameter("rows") != null) {
            query.setPagesize(Integer.valueOf(request.getParameter("rows")).intValue());
        }
        if (request.getParameter("sort") != null) {
            query.addSort(new DBSort(request.getParameter("sort"), request.getParameter("order")));
        }

        return query;
    }
}
